package com.zzyl.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.zzyl.constant.Constants;
import com.zzyl.dto.UserLoginRequestDto;
import com.zzyl.entity.Member;
import com.zzyl.mapper.MemberMapper;
import com.zzyl.properties.JwtTokenManagerProperties;
import com.zzyl.service.MemberService;
import com.zzyl.service.WeChatService;
import com.zzyl.utils.JwtUtil;
import com.zzyl.vo.LoginVo;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.assertj.core.util.Lists;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

@Service
@RequiredArgsConstructor
public class MemberServiceImpl implements MemberService {

    private final WeChatService weChatService;

    private final MemberMapper memberMapper;

    private final JwtTokenManagerProperties jwtTokenManagerProperties;

    static ArrayList DEFAULT_NICKNAME_PREFIX = Lists.newArrayList(
            "生活更美好",
            "大桔大利",
            "日富一日",
            "好柿开花",
            "柿柿如意",
            "一椰暴富",
            "大柚所为",
            "杨梅吐气",
            "天生荔枝"
    );
    @Override
    public LoginVo login(UserLoginRequestDto userLoginRequestDto) {
        //通过code获取openID
        String openId = weChatService.getOpenId(userLoginRequestDto.getCode());
        //通过phoneCode获取手机号码
        String phoneNumber = weChatService.getPhoneNumber(userLoginRequestDto.getPhoneCode());
        //通过openID查询数据库
        Member member = memberMapper.selectByOpenId(openId);
        //信息存在,判断手机号是否一致,不一致更新
        if (null != member){
            String phone = member.getPhone();
            if (!ObjectUtil.equal(phone, phoneNumber)){
                Member updateMember = new Member();
                updateMember.setPhone(phoneNumber);
                updateMember.setId(member.getId());
                memberMapper.update(updateMember);
            }
        }else {
                //信息不存在,新增
                Member insterMember = Member.builder().name(getRandomNickName(phoneNumber))
                        .openId(openId)
                        .phone(phoneNumber)
                        .build();
                memberMapper.insert(insterMember);
            }

        //生成token
        Map<String, Object> claims = new HashMap<>();
        assert member != null;
        claims.put(Constants.JWT_MEMBER_ID, member.getId());
        claims.put(Constants.JWT_USERNAME, member.getName());
        String token = JwtUtil.createJWT(jwtTokenManagerProperties.getBase64EncodedSecretKey(),
                jwtTokenManagerProperties.getTtl(), claims);
        //构建vo返回
        return LoginVo.builder()
                .token(token)
                .nickName(member.getName())
                .build();
    }

    /**
     * 随机生成昵称
     * @param phoneNumber
     * @return
     */
    private String getRandomNickName(String phoneNumber ){
        return DEFAULT_NICKNAME_PREFIX.get((int) (Math.random() * DEFAULT_NICKNAME_PREFIX.size()))
                + StringUtils.substring(phoneNumber, 7);
    }
}
